import store from "@/store";
import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    name: "home",
    component: Home,
  },
  {
    path: "/goods",
    name: "goods",

    component: () =>
      import(/* webpackChunkName: "goods" */ "../views/Goods.vue"),
  },
  {
    path: "/user",
    name: "user",
    component: () => import(/* webpackChunkName: "user" */ "../views/User.vue"),
    redirect: "/user/Cart",
    children: [
      {
        path: "cart",
        component: () =>
          import(/* webpackChunkName: "user" */ "../components/user/Cart.vue"),
      },
    ],

    // beforeEnter: (to, from, next) => {
    //   // ...路由独享守卫 输出看一下
    //   console.log("user-路由独享守卫...", to, from);

    //   let token = localStorage.getItem("x-auth-token");
    //   // 判断token  如果有token 就表示用户已经登录,就正常进入user
    //   if (token) {
    //     // 有token正常进入  next()
    //     next();
    //   } else {
    //     alert("请先登录");
    //   }
    // },
  },
  {
    path: "/order",
    name: "order",

    component: () =>
      import(/* webpackChunkName: "order" */ "../views/Order.vue"),
  },
  {
    path: "/free",
    name: "free",

    component: () =>
      import(/* webpackChunkName: "order" */ "../views/Free.vue"),
  },
  {
    path: "/details",
    name: "details",

    component: () =>
      import(/* webpackChunkName: "details" */ "../views/Details.vue"),
  },
  {
    path: "*",
    name: "error",
    component: () =>
      import(/* webpackChunkName: "error" */ "../components/Error.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});
//  全局守卫..........................................
// router.beforeEach((to, from, next) => {
//   // next不能少
//   // console.log(to, from);
//   // next();
//   // ................................
//   // 全局守卫;
//   // 当去到user的页面是.需要进行是否登录
//   if (to.path == "/user") {
//     console.log("打开user页面");
//     let koken = localStorage.getItem("x-auth-token");
//     // 判断token  如果有token 就表示用户已经登录,就正常进入user
//     if (token) {
//       // 有token正常进入  next()
//       next();
//     } else {
//       // alert('请先登录')
//       // 没有token
//       store.dispatch("showToast/asyncChangeIsShowToast", {
//         isShow: true,
//         msg: "请先登录",
//         type: "danger",
//       });
//     }
//     // 返回
//     return;
//   }
//   // 继续执行
//   next();
// });
// .................................................全局守卫(上)

// 路由独享守卫......下

// 解决连续点击路由会报错的情况.......................
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch((err) => {});
};
export default router;
